<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends SMB_Controller {
	
	public function __construct() {
		parent::__construct();
	}
	
	public function index() {
		$this->_get_userinfo();
		$this->_goto_userpage();
		
		$this->form_validation->set_error_delimiters('<span class="error">', "</span>");
		if ($this->form_validation->run('login') == FALSE) {
			$this->_load_common_header();
			$this->_load_common_navigation();
			$params = array(
				'error' => $this->session->flashdata('error'),
				'username' => $this->session->flashdata('username')
			);
			$this->load->view('login', $params);
			$this->_load_common_footer();
		} else {
			$username = $this->input->post('username', TRUE);
			$password = $this->input->post('password', TRUE);
			$this->_login($username, $password);
		}
	}
	
	private function _login($username, $password) {
		$result = $this->authentication->login($username, $password);
		$this->_get_userinfo();
		if ($result === FALSE) {
			$this->session->set_flashdata('error', 'Username dan Password tidak cocok.');
			$this->session->set_flashdata('username', $username);
			redirect('home/index', 'refresh');
		} else {
			$this->_goto_home();
		}
	} 
	
	public function logout() {
		$this->authentication->logout();
		$this->_get_userinfo();
		$this->_goto_home();
	}
	
	public function test() {
		$username = 'riyan.noor';
		$password = 'jssani462775';
		$conn = @ldap_connect("152.118.39.37") or die('could not connect to the server');
        $opt = @ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
        if($conn) {
			echo "hatch";
            $filter = "uid='".$username."'";
            $result = ldap_search($conn, "o=Universitas Indonesia,c=ID", $filter);
            $info   = ldap_get_entries($conn, $result);
            
            if ($info['count'] == 0) {
				echo "count";
                ldap_close($conn);
            }
            
            $nama = $info[0]['cn'][0];
            $npm  = $info[0]['idmhs'][0];
            
            $DN  = $info[0]["dn"];
            $ret = @ldap_bind($conn, $DN, $password);
            ldap_close($conn);
            
            if(!$ret) {
				echo "passsalah";
            } else {
				$data = array(
					'nama' => $nama,
					'kode' => $npm,
					'jabatan' => "penghuni",
					'logged_in' => true
				);
				var_dump($data);
			}
        } else {
            ldap_close($conn);//koneksi gagal
            echo 'koneksi gagal';
        }
	}
	
	public function webservice($npm) {
		$url = 'https://services.ui.ac.id/ws/middleware/?f=PendaftaranBeasiswa&args=2&1='.$npm.'&2=20101&wkt=now';
		$result= json_decode(file_get_contents ($url),true);
		print_r($result);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */